package com.stu.mall.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CompletableFutureTest {

    public static ExecutorService service = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main.....start.....");
//        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果:" + Thread.currentThread().getId() + "=" + i);
//        }, service);


//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果:" + Thread.currentThread().getId() + "=" + i);
//            return i;
//        }, service).whenComplete((res, exception) -> {
//            //可以感知异常，但是不允许默认返回
//            System.out.println("结果是:" + res);
//            System.out.println("异常是:" + exception);
//        }).exceptionally((exception) -> {
//            System.out.println("异常是:" + exception);
//            //当出现异常，默认返回
//            return 10;
//        });

//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果:" + Thread.currentThread().getId() + "=" + i);
//            return i;
//        }, service).handleAsync((res, thr) -> {
//            if (res != null) {
//                System.out.println("获得响应值:"+res);
//                return res;
//            }
//
//            if (thr != null) {
//                System.out.println("出现异常:"+thr);
//                return 0;
//            }
//
//            return 0;
//        });


//        CompletableFuture<Void> future1 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果:" + Thread.currentThread().getId() + "=" + i);
//            return i;
//        }, service).thenAcceptAsync((res) -> {
//            System.out.println("启动任务1:" + res);
//        }, service);

//        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果:" + Thread.currentThread().getId() + "=" + i);
//            return i;
//        }, service).thenApplyAsync((res) -> {
//            System.out.println("启动任务1:" + res);
//            return res;
//        }, service);


        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1结束.......");
            return 1;
        }, service);

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2结束.......");
            return 2;
        }, service);

        CompletableFuture<Integer> future3 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务3结束.......");
            return 3;
        }, service);

        CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(future1, future2, future3);
        allOfFuture.get();//等待所有结果返回

        System.out.println("所有任务都执行完了。。。");

//        future1.runAfterBothAsync(future2, () -> {
//            System.out.println("任务3开始.......");
//            System.out.println("任务3结束.......");
//        }, service);


//        future1.thenAcceptBothAsync(future2, (res1, res2) -> {
//            System.out.println("任务3开始.......");
//            System.out.println("future1返回值:" + res1);
//            System.out.println("future2返回值:" + res2);
//            System.out.println("任务3结束.......");
//        }, service);

//        CompletableFuture<Integer> future3 = future1.thenCombineAsync(future2, (res1, res2) -> {
//            System.out.println("任务3开始.......");
//            System.out.println("future1返回值:" + res1);
//            System.out.println("future2返回值:" + res2);
//            System.out.println("任务3结束.......");
//
//            return res1 + res2;
//        }, service);
//
//        System.out.println("future3返回值:" + future3.get());

        System.out.println("main.....end.....");
    }
}
